package com.linkoog.devtools.dubbo.toolwindow.workbenche;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.components.JBLoadingPanel;
import com.linkoog.devtools.dubbo.raw_index.DubboMethod;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.DubboTestLeftPanel;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.right.DubboTestRightPanel;

import javax.swing.*;
import java.awt.*;

public class DubboWorkbencheMainPanel implements Disposable {
    private Project project;


    private JPanel myMainPanel;

    private final JBLoadingPanel loadingPanel;
    private final DubboTestLeftPanel dubboTestLeftPanel;
    private final DubboTestRightPanel dubboTestRightPanel;

    public DubboWorkbencheMainPanel(Project project) {
        this.project = project;

        loadingPanel = new JBLoadingPanel(new BorderLayout(), this);
        loadingPanel.add(myMainPanel, BorderLayout.CENTER);

        dubboTestLeftPanel = new DubboTestLeftPanel(project);
        Disposer.register(this, dubboTestLeftPanel);
        dubboTestRightPanel = new DubboTestRightPanel(project, loadingPanel);
        Disposer.register(this, dubboTestRightPanel);

        //初始化点击事件
        dubboTestLeftPanel.setFavoritesClickCallback(dubboTestRightPanel::resetPanel);
        dubboTestLeftPanel.setHistoryClickCallback(dubboTestRightPanel::resetPanel);
        dubboTestRightPanel.setSendClickCallback(dubboTestLeftPanel::addHistory);
        dubboTestRightPanel.setSaveClickCallback(dubboTestLeftPanel::addFavorites);

        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.2F);
        //splitter.setResizeEnabled(false);
        splitter.setDividerPositionStrategy(Splitter.DividerPositionStrategy.KEEP_FIRST_SIZE);
        splitter.setFirstComponent(dubboTestLeftPanel.getMyMainPanel());
        splitter.setSecondComponent(dubboTestRightPanel.getMyMainPanel());
        this.myMainPanel.add(splitter, BorderLayout.CENTER);
    }

    @Override
    public void dispose() {
        if (loadingPanel.isLoading()) {
            loadingPanel.stopLoading();
        }
    }

    public void resetPanel(DubboMethod dubboMethod){
        dubboTestRightPanel.resetPanel(dubboMethod);
    }

    public JPanel getMyMainPanel() {
        return loadingPanel;
    }
}
